import { login, logout } from "@/api/user"
import store from 'store'

export default {
    namespaced: true,
    state: {
        userInfo: store.get('userInfo'), 
        isValidLogin:false
    },
    mutations: {
        setUserInfo(state, data) {
            state.userInfo = data;
            store.set('userInfo', data)
            if (data) {
                state.isValidLogin = true
            } else {
                state.isValidLogin = false
            }
        },
        removeUserInfo(state){
            state.userInfo = {};
            store.remove('userInfo')
        }
    },
    actions: {
        login({ commit}, data) {
            return login(data).then(res=>{
                console.log(res.data);
                commit('setUserInfo', res.data);
            })
        },
        logout({ commit},data) {
            return logout(data).then(res=>{
                  console.log(res);      
                   commit('removeUserInfo');
            });    
        }
    }
}